package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.UserFollowMapper;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.pojo.UserFollow;
import com.wenwan.manage.service.UserFollowService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class UserFollowServiceImpl implements UserFollowService {

	@Autowired
	private UserFollowMapper userFollowMapper;

	@Override
	public Map<String, Object> create(String followerUserId, String beFollowerUserId) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (followerUserId != null && !followerUserId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "followerUserId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("followerUserId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (beFollowerUserId != null && !beFollowerUserId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "beFollowerUserId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("beFollowerUserId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", DigestUtils.md5Hex(followerUserId + beFollowerUserId).toUpperCase());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("followerUserId", followerUserId);
		map.put("beFollowerUserId", beFollowerUserId);

		Boolean data = userFollowMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Boolean data = userFollowMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize, String userId, Boolean isFollower,
			Boolean isBeFollower) {
		// TODO Auto-generated method stub
		// DataToObject<List<UserFollow>> dto = new DataToObject<List<UserFollow>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("isFollower", isFollower);
		map.put("isBeFollower", isBeFollower);

		List<UserFollow> data = userFollowMapper.selectAll(map);

		LimitPage limitPage = new LimitPage();
		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);
		Integer maxSize = userFollowMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setLimitPage(limitPage);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("data", limitPage);
		dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
		dto.put("code", StatusSetting.CODE_10050);
		return dto;
	}

}
